{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# File Handling ... \n",
    "\n",
    "\n",
    "in computer/ smart phones all the bytes are stored in contrainers, \n",
    "\n",
    "which can be accessed any point of time whenever we need them \n",
    "\n",
    "and yes we can store multiple containers together in a bundles. \n",
    "\n",
    "now what are they ??\n",
    "\n",
    "1. Containers are files ?\n",
    "2. Bundles are Folders ?\n",
    "\n",
    "\n",
    "# what all things we can do with File Handling ?\n",
    "\n",
    "1. read \n",
    "2. write \n",
    "3. create new file \n",
    "4. modify the file \n",
    "\n",
    "\n",
    "Now lets go ahead and learn how to handle files in Python  .... \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a file ?\n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"w\")\n",
    "\n",
    "file.close()\n",
    "\n",
    "#VaribaleName = open(FileName,Mode)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Write data to File \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"w\")\n",
    "\n",
    "file.write(\"HI Guys we are writing inside a file\")\n",
    "\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OYEEE\n",
      "\n",
      "rafda\n",
      "\n",
      "hey guys i hope u are not sleeping right now !!\n"
     ]
    }
   ],
   "source": [
    "# Reading a file from the Computer \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"r\")\n",
    "someText = file.read()\n",
    "print(someText)\n",
    "#file.write(\"OYEEE\") this will give error \n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add some lines to the File \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"a\")\n",
    "\n",
    "file.write(\"\\n\\nThis is some more data!!\")\n",
    "\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI Guys we are writing inside a file\n",
      "\n",
      "This is some more data!!\n"
     ]
    }
   ],
   "source": [
    "# r+ mode, reading and writing both \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"r+\")\n",
    "print(file.read())\n",
    "file.write(\"\\n\\nThanks guys for trusting me \")\n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI Guys we are writing inside a file\n",
      "\n",
      "This is some more data!!\n",
      "\n",
      "Thanks guys for trusting me \n"
     ]
    }
   ],
   "source": [
    "# Reading a file from the Computer \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"r\")\n",
    "someText = file.read()\n",
    "print(someText)\n",
    "#file.write(\"OYEEE\") this will give error \n",
    "file.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reading a file from the Computer \n",
    "\n",
    "file = open(\"fantasticFive.txt\",\"w\")\n",
    "file.write(\"OYEEE\") #this will give error \n",
    "file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# With function in File Handling \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hey guys lets enter something herehey guys i hope u are not sleeping right now !!\n"
     ]
    }
   ],
   "source": [
    "with open(\"fantasticFive.txt\",\"a\") as file:  \n",
    "    someInput = input(\"Hey guys lets enter something here\")\n",
    "    file.write(\"\\n\\n\"+someInput) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Methods ??\n",
    "\n",
    "Functions which are by default in Python and Functions which are inside objects are knows as methods \n",
    "\n",
    "and we have already seen together List and its methods\n",
    "\n",
    "\n",
    "# Functions ??\n",
    "\n",
    "Functions are the resuale coding block, which are user defined for making sure the code repeatablity is encontered and reduced"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lst = [1,2,4,5,6]\n",
    "# Methods \n",
    "lst.index(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- How to write functions ?\n",
    "- what all things we can do with functions?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " HI Deepak\n",
      " HI Akash\n",
      " HI Krishnaveni\n",
      " HI Gayatri\n",
      " HI Bharat\n",
      " HI Carla\n"
     ]
    }
   ],
   "source": [
    "print(\" HI\", \"Deepak\")\n",
    "print(\" HI\", \"Akash\")\n",
    "print(\" HI\", \"Krishnaveni\")\n",
    "print(\" HI\", \"Gayatri\")\n",
    "print(\" HI\", \"Bharat\")\n",
    "print(\" HI\", \"Carla\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Defining a code block inside function in Python\n",
    "\n",
    "def someFunctionName(argument):\n",
    "    print(\"-------------\")\n",
    "    print(\"Hi\",argument)\n",
    "    print(\"-------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "Hi Deepak\n",
      "-------------\n"
     ]
    }
   ],
   "source": [
    "someFunctionName(\"Deepak\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [\"deepak\",\"carla\",\" het\", \" muneer\", \" vivek\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "Hi deepak\n",
      "-------------\n",
      "-------------\n",
      "Hi carla\n",
      "-------------\n",
      "-------------\n",
      "Hi  het\n",
      "-------------\n",
      "-------------\n",
      "Hi  muneer\n",
      "-------------\n",
      "-------------\n",
      "Hi  vivek\n",
      "-------------\n"
     ]
    }
   ],
   "source": [
    "for item in lst:\n",
    "    someFunctionName(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Where we use Return Keyword ???\n",
    "\n",
    "\n",
    "# when ever you want to process some kind of data in function \n",
    "# and return that final value from your function that time \n",
    "# we use Return keyword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# function to return if my number is odd or even \n",
    "\n",
    "def oddEvenFun(num):\n",
    "    if num%2 == 0 :\n",
    "        text = str(num) + \" is Even\"\n",
    "        return text\n",
    "    else:\n",
    "        text = str(num) + \" is Odd\"\n",
    "        return text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 is Odd\n",
      "2 is Even\n",
      "3 is Odd\n",
      "4 is Even\n",
      "5 is Odd\n",
      "6 is Even\n",
      "7 is Odd\n",
      "8 is Even\n",
      "9 is Odd\n"
     ]
    }
   ],
   "source": [
    "for item in range(1,10):\n",
    "    print(oddEvenFun(item))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "# args*\n",
    "\n",
    "# kwargs**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "def greeting(*name):\n",
    "    for i in name:\n",
    "        print(\"Hi \",i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi  saikiran\n"
     ]
    }
   ],
   "source": [
    "greeting(\"saikiran\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [],
   "source": [
    "#**Kwargs ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [],
   "source": [
    "def amigo(**kwargs):\n",
    "    for key,value in kwargs.items():\n",
    "        print(key,\" \", value)\n",
    "    print(\" \")\n",
    "    print(\" \")\n",
    "    print(kwargs.get(\"location\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name   Saikiran\n",
      "age   65\n",
      "location   Navi Mumbai\n",
      " \n",
      " \n",
      "Navi Mumbai\n"
     ]
    }
   ],
   "source": [
    "amigo(name = \"Saikiran\", age=\"65\", location = \"Navi Mumbai\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [],
   "source": [
    "def printingPaper(*papers):\n",
    "    for paper in papers:\n",
    "        print(paper)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a41\n",
      "a46\n",
      "a49\n"
     ]
    }
   ],
   "source": [
    "printingPaper(\"a41\",\"a46\",\"a49\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
